package com.siae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.UniversityMapper;
import com.siae.pojo.University;
import com.siae.pojo.vo.Result;
import com.siae.service.UniversityService;
import com.siae.utils.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UniversityServiceImpl extends ServiceImpl<UniversityMapper, University> implements UniversityService {
    @Autowired
    private UniversityMapper universityMapper;

    @Override
    public Result<List<University>> findAllUniversity() {
        try {
            List<University> universities = universityMapper.selectList(null);
            return Result.success(BizCode.SUCCESS,universities);
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取大学信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<University> getUniversityById(Integer id) {
        try {
            University university = universityMapper.selectById(id);
            return Result.success(BizCode.SUCCESS,university);
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取大学信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<List<University>> getUniversityByName(String name) {
        try {
            QueryWrapper<University> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(name),"university_name",name);
            List<University> universities = universityMapper.selectList(wrapper);
            return Result.success(BizCode.SUCCESS,universities);
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取大学信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }
}
